
;window.Teamlab = (function (Teamlab) {
  var
    isInit = false;

  var customEvents = {
    discussionPage  : 'ondiscussionpage',
    discussionsPage : 'ondiscussionspage',

    discussionsLoadDiscussions : 'ondiscussionsloaddiscussions'
  };
  Teamlab.extendEventManager(customEvents);

  var CallbackMethods = {
    discussions_add_comment : function (params, comment) {
      comment.encodetext = encodeURI(comment.text);
      var commentType = params.hasOwnProperty('___commenttype') ? params.___commenttype : null;
      if (commentType) {
        Teamlab.call(Teamlab.events.addComment, window, [params, {type : commentType, parentid : comment.parentid, item : comment}])
      }
    },
    discussions_update_comment : function (params, comment) {
      comment.encodetext = encodeURI(comment.text);
      var commentType = params.hasOwnProperty('___commenttype') ? params.___commenttype : null;
      if (commentType) {
        Teamlab.call(Teamlab.events.updateComment, window, [params, {type : commentType, parentid : comment.parentid, item : comment}])
      }
    },
    discussions_remove_comment : function (params, comment) {
      comment.encodetext = encodeURI(comment.text);
      var commentType = params.hasOwnProperty('___commenttype') ? params.___commenttype : null;
      if (commentType) {
        Teamlab.call(Teamlab.events.removeComment, window, [params, {type : commentType, parentid : comment.parentid, item : comment}]);
      }
    },
    discussions_load_discussions : function (params, discussions) {
      var
        discussion = null,
        discussionsInd = discussions.length;
      while (discussionsInd--) {
        discussion = discussions[discussionsInd];
        discussion.link = 'discussions/' + discussion.id;
      }
      Teamlab.call(Teamlab.events.discussionsLoadDiscussions, window, [params, {items : discussions}]);
    },

    discussions_load_selfprojects : function (params, projects) {
      Teamlab.call(Teamlab.events.discussionsPage, window, [params, {pagemenu : Teamlab.getPageMenu('discussions', 0), items : projects}]);
    },

    discussions_load_discussion : function (params, discussion, comments) {
      var comments = [].concat(discussion.comments);
      delete discussion.comments;
      comments.unshift(discussion);
      var commentsInd = comments.length;
      while (commentsInd--) {
        comments[commentsInd].encodetext = encodeURI(comments[commentsInd].text);
      }
      Teamlab.call(Teamlab.events.discussionPage, window, [params, {id : discussion.id, pagemenu : Teamlab.getPageMenu('discussions', 0), items : Teamlab.sortCommentsByTree(comments)}]);
      delete comments;
      comments = null;
    }
  };

  Teamlab.bind(Teamlab.events.discussionAnch, onDiscussionAnch);
  Teamlab.bind(Teamlab.events.discussionsAnch, onDiscussionsAnch);

  function onDiscussionAnch (params, id) {
    Teamlab.getPrjDiscussion({___handler : 'discussions-load-discussion', ___container : CallbackMethods}, id);
  }

  function onDiscussionsAnch (params) {
    Teamlab.discussionsLoadSelfProjects();
  }

  Teamlab.discussionsLoadSelfProjects = function () {
    return Teamlab.getPrjSelfProjects({___handler : 'discussions-load-selfprojects', ___container : CallbackMethods});
  };

  Teamlab.discussionsLoadDiscussionsByProject = function (projectid) {
    return Teamlab.getPrjDiscussions({___handler : 'discussions-load-discussions', ___container : CallbackMethods}, projectid);
  };

  Teamlab.discussionsAddComment = function (id, parentid, subject, content) {
    var data = {
      id : id,
      parentid : parentid,
      subject : subject,
      content : content
    };
    var commentType = 'discussion';
    return Teamlab.addPrjComment({___handler : 'discussions-add-comment', ___container : CallbackMethods, ___commenttype : commentType}, commentType, id, data);
  };

  Teamlab.discussionsUpdateComment = function (id, parentid, subject, content) {
    var data = {
      id : id,
      parentid : parentid,
      subject : subject,
      content : content
    };
    var commentType = 'discussion';
    return Teamlab.updatePrjComment({___handler : 'discussions-update-comment', ___container : CallbackMethods, ___commenttype : commentType}, commentType, id, data);
  };

  Teamlab.discussionsRemoveComment = function (id) {
    var commentType = 'discussion';
    return Teamlab.removePrjComment({___handler : 'discussions-remove-comment', ___container : CallbackMethods, ___commenttype : commentType}, commentType, id);
  };

  return Teamlab;
})(Teamlab);
